

public class Main {
    static class ListNode{
        public int val;
        public ListNode next;

        //构造方法
        public ListNode(int val) {
            this.val = val;
        }
    }

    //头结点
    public ListNode head;

    public void creatList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;//头结点是node1
    }

    //打印链表
    public void show() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


    public int kthToLast2(int k) {
        if(k <= 0 || k > size()) {
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = k - 1;
        while(count != 0) {
            fast = fast.next;
            if (fast == null) {
                return -1;
            }
            count--;
        }
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    public int kthToLast(ListNode head, int k) {
        if(k <= 0 || k > size()) {
            return -1;
        }
        ListNode cur = head;
        int count = size() - k;
        while(count != 0) {
            cur = cur.next;
            count--;
        }
        return cur.val;
    }

    public ListNode findMiddleNode2() {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null  ) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    /*public ListNode findMiddleNode() {
        int len = size() / 2;
        ListNode cur = head;
        while(len != 0) {
            cur = cur.next;
            len--;
        }
        return cur;
    }*/

    public void addIndex(int index,int val) {

    }


    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        //cur.next == null
        cur.next = node;
    }


    public ListNode partition(int x) {
        ListNode cur = head;

        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    bs = cur;
                    be = bs;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = cur;
                    ae = as;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null) {
            return as;
        }
        be.next = as;
        if (ae.next != null) {
            ae.next = null;
        }
        return bs;
    }


    public ListNode findMiddleNode() {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    //检查回文
    public boolean chkPalindrome() {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //反转
        ListNode cur = slow;
        ListNode curN = slow;
        while (cur != null) {
            curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        //判断
        /*while (head.val == slow.val) {
            head = head.next;
            slow = slow.next;
            if (slow == head) {
                return true;
            }
        }*/
        return false;
    }


    public int size(ListNode head) {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pl = headA;
        ListNode ps = headB;

        int lenA = size(headA);
        int lenB = size(headB);

        if (lenA > lenB) {
            pl = headA;
            ps = headB;
        } else {
            ps = headA;
            pl = headB;
        }

        int len = Math.abs(lenA - lenB);

        while (len != 0) {
            pl = pl.next;
            len--;
        }
        while(ps != pl) {
            ps = ps.next;
            pl = pl.next;
            if (pl == null) {
                return null;
            }
        }
        return pl;
    }


    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }



    /*public ListNode mergeTwoLists(ListNode headA, ListNode headB) {
        ListNode newH = new ListNode();
        ListNode tmpH = newH;
        while(headA != null && headB != null) {
            if (headA.val < headB.val) {
                tmpH.next = headA;
                tmpH = tmpH.next;
                headA = headA.next;
            } else {
                tmpH.next = headB;
                tmpH = tmpH.next;
                headB = headB.next;
            }
        }
        if (headA == null) {
            headA.next = headB;
        } else {
            headB.next = headA;
        }
        return newH;
    }*/

    public static void main(String[] args) {

    }
}